﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KarReq.Domain.Abstract;
using KarReq.WebUI.Models;

namespace KarReq.WebUI.Controllers
{
    [Authorize]
    public class KaraokeTrackController : KarReqController
    {
        public int PageSize = 100; // We will change this later

        public KaraokeTrackController(IKaraokeTrackRepository repo) : base(repo){}

        public ViewResult List(string category, int page = 1)
        {
             KaraokeTracksListViewModel viewModel = new KaraokeTracksListViewModel
            {
                KaraokeTracks = repository.KaraokeTracks
                    .Where(p => category == null || p.Artist.Substring(0, 1) == category)
                    .OrderBy(p => p.Artist)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = category == null ?
                        repository.KaraokeTracks.Count() :
                        repository.KaraokeTracks.Where(e => e.Artist.Substring(0,1) ==
                            category).Count()
                },
                CurrentCategory = category
            };
            return View(viewModel);
        }

        public ViewResult Search(string search, int page = 1)
        {
            KaraokeTracksListViewModel viewModel = new KaraokeTracksListViewModel
            {
                KaraokeTracks = repository.KaraokeTracks
                    .Where(p => search == null || p.Artist.Substring(0, 1) == search)
                    .OrderBy(p => p.Artist)
                    .Skip((page - 1) * PageSize)
                    .Take(PageSize),
                PagingInfo = new PagingInfo
                {
                    CurrentPage = page,
                    ItemsPerPage = PageSize,
                    TotalItems = search == null ?
                        repository.KaraokeTracks.Count() :
                        repository.KaraokeTracks.Where(e => e.Artist.Substring(0, 1) ==
                            search).Count()
                },
                CurrentCategory = search
            };
            return View(viewModel);
        }
    }
}
